<?php
namespace app\api\controller;

use app\admin\model\arearecord\Arearecord;
use app\admin\model\dayrechargerecord\Dayrechargerecord;
use app\admin\model\fanfurecord\Fanfurecord;
use app\admin\model\qianghongbao\Qianghongbao;
use app\admin\model\sendhongbao\Sendhongbao;
use app\admin\model\shouchongjiang\Shouchongjiang;
use app\admin\model\teamrecord\Teamrecord;
use app\admin\model\useraccount\Useraccount;
use app\common\controller\Api;
use think\Db;
use think\Exception;
use think\Session;

/**用户资产**/
class My extends Api{
    protected $noNeedLogin = ['setpaypassword','findpassword'];
    protected $noNeedRight = ['*'];

    /**
     * 我的资产
     */
    public function zichangdetail(){
        $userinfo = $this->auth->getUserinfo();
        $backData = [
            'jingtai_amount'=>$userinfo['jingtai_amount'],
            'tuiguang_amount'=>$userinfo['tuiguang_amount'],
            'tuandui_amount'=>$userinfo['tuandui_amount'],
            'quyu_amount'=>$userinfo['quyu_amount'],
            'fanfu_amount'=>$userinfo['fanfu_amount'],
            'hcc_balance'=>$userinfo['hcc_balance'],
            'hcbao_amount'=>$userinfo['hcbao_amount'],
            'jianshebao_amount'=>$userinfo['jianshebao_amount'],
            'hc_hongbao_amount'=>$userinfo['hc_hongbao_amount'],
            'hcc_hongbao_amount'=>$userinfo['hcc_hongbao_amount'],
        ];
        $this->success('成功',$backData);
    }

    /**静态收益列表**/
    public function jingtailist(){
        $page = $this->request->param('page',1);
        $pageSize = 10;
        $start=($page-1)*$pageSize;
        $userinfo = $this->auth->getUserinfo();
        $dayrechargeModel = new Dayrechargerecord();
        $list = collection($dayrechargeModel->where(['user_id'=>$userinfo['id']])->order('id desc')->select());
        if($list){
            $list = $list->toArray();
            $this->success('成功',$list);
        }else{
            $this->success('成功',[]);
        }
    }

    /**
     * 推广收益列表
     **/
    public function tuiguanglist(){
        $userinfo = $this->auth->getUserinfo();
        $page = $this->request->param('page',1);
        $pageSize = 10;
        $start = ($page-1)*$pageSize;
        $shouchongjiangModel = new Shouchongjiang();

        // 使用join关联user表
        $list = collection($shouchongjiangModel
            ->alias('s')
            ->join('user u', 'u.id = s.from_user_id')
            ->field('s.*, u.nickname as from_nickname')
            ->where(['s.to_user_id' => $userinfo['id']])
            ->order('s.create_time desc')
            ->limit($start,$pageSize)
            ->select());

        if($list){
            $list = $list->toArray();
            $newlist =[];
            foreach ($list as $k=>$v){
                $newlist[$k]['amount']=$v['jiang_amount'];
                $newlist[$k]['from_nickname']=$v['from_nickname'];
                $newlist[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
            }
            $this->success('成功', $newlist);
        }else{
            $this->success('成功', []);
        }
    }

    /**
     * 团队收益列表
     **/
    public function tuanduilist(){
        $userinfo = $this->auth->getUserinfo();
        $page = $this->request->param('page',1);
        $pageSize = 10;
        $start = ($page-1)*$pageSize;
        $teamrecordModel = new Teamrecord();
        $list = collection($teamrecordModel->where(['user_id'=>$userinfo['id']])->limit($start,$pageSize)->order('id desc')->select());
        if($list){
            $list = $list->toArray();
            $this->success('成功',$list);
        }else{
            $this->success('成功',[]);
        }
    }

    /**区域收益列表**/
    public function arealist(){
        $userinfo = $this->auth->getUserinfo();
        $page = $this->request->param('page',1);
        $pageSize = 10;
        $start = ($page-1)*$pageSize;
        $arearecordModel = new Arearecord();
        $list = collection($arearecordModel->where(['user_id'=>$userinfo['id']])->limit($start,$pageSize)->order('id desc')->select());
        if($list){
            $list = $list->toArray();
            $this->success('成功',$list);
        }else{
            $this->success('成功',[]);
        }
    }

    /**反哺奖列表**/
    public function fanfulist(){
        $userinfo = $this->auth->getUserinfo();
        $page = $this->request->param('page',1);
        $pageSize = 10;
        $start = ($page-1)*$pageSize;
        $fanfurecordModel = new Fanfurecord();
        $list = collection($fanfurecordModel->where(['to_user_id'=>$userinfo['id']])->limit($start,$pageSize)->order('id desc')->select());
        if($list){
            $list = $list->toArray();
            $this->success('成功',$list);
        }else{
            $this->success('成功',[]);
        }
    }

    /**设置安全密码**/
    public function setpaypassword(){
        $userinfo = $this->auth->getUserinfo();
        $pay_password = $this->request->param('pay_password');
        $code = $this->request->param('code');
        $pay_password = md5($pay_password);
        $sess_code = Session::get('ANQUAN_CODE');
        if($sess_code != $code){
            $this->error('验证码不正确');
        }
        $userModel = new \app\admin\model\User();
        $userModel->where(['id'=>$userinfo['id']])->update([
            'pay_password'=>$pay_password,
        ]);
        $this->success('设置成功');
    }

    /**修改密码**/
    public function findpassword(){
        $account = $this->request->param('account');
        $password = $this->request->param('password');
        $sure_password = $this->request->param('sure_password');
        $code = $this->request->param('code');
        $userModel = new \app\admin\model\User();
        $info = $userModel->where(['username'=>$account])->find();
        if(!$info){
            $this->error('账号不存在');
        }
        $info = $info->toArray();
        if(mb_strlen($password,'utf-8') <6 || mb_strlen($password,'utf-8')>20){
            $this->error('密码不得小于6位大于20位');
        }
        if($password != $sure_password){
            $this->error('密码和确认密码不一致');
        }
        $sess_code = Session::get('FIND_CODE');
        if($sess_code!=$code){
            $this->error('验证码不正确');
        }
        $password = md5(md5($password).$info['salt']);
        $userModel->where(['id'=>$info['id']])->update(['password'=>$password]);
        $this->success('找回密码成功');
    }

    public function updatepassword(){
        $id = $this->request->param('id');
        $password = $this->request->param('password');
        $newpassword = $this->request->param('newpassword');
        $sure_newpassword = $this->request->param('sure_newpassword');
        if($newpassword!=$sure_newpassword){
            $this->error('密码和确认密码不一致');
        }
        $userinfo = $this->auth->getUserinfo();
        if($id!=$userinfo['id']){
            $this->error('您的ID不正确');
        }
        $userModel = new \app\admin\model\User();
        $userinfo = $userModel->where(['id'=>$id])->find()->toArray();
        $password = md5(md5($password).$userinfo['salt']);
        if($password != $userinfo['password']){
            $this->error('旧密码不正确');
        }
        $newpassword=md5(md5($newpassword).$userinfo['salt']);
        $userModel->where(['id'=>$id])->update(['password'=>$newpassword]);
        $this->success('修改成功');
    }

    /**发红包纪示**/
    public function sendhongbaolist(){
        $page =$this->request->param('page',1);
        $pageSize=10;
        $start=($page-1)*$pageSize;
        $userinfo =$this->auth->getUserinfo();
        $sendhongbaoModel = new Sendhongbao();
        $list = collection($sendhongbaoModel->where(['user_id'=>$userinfo['id']])->limit($start,$pageSize)->order('id desc')->select());
        if($list){
            $list = $list->toArray();
            $newlist = [];
            foreach ($list as $k=>$v){
                $newlist[$k]['id']=$v['id'];
                $newlist[$k]['amount']=$v['amount'];
                $newlist[$k]['nums']=$v['nums'];
                $newlist[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
                $newlist[$k]['type']=$v['type'];
            }
            $this->success('成功',$newlist);
        }else{
            $this->success('成功',[]);
        }
    }

    public function qianghongbaolist(){
        $page = $this->request->param('page', 1);
        $pageSize = 10;
        $userinfo = $this->auth->getUserinfo();

        $qianghongbaoModel = new Qianghongbao();

        // 使用JOIN关联查询
        $list = $qianghongbaoModel
            ->alias('q')
            ->join('sendhongbao s', 'q.hongbao_id = s.id')
            ->field('q.*, s.type') // 获取qianghongbao表所有字段和sendhongbao表的type字段
            ->where(['q.user_id' => $userinfo['id']])
            ->page($page, $pageSize)
            ->order('q.id desc')
            ->select();

        if($list){
            $list = collection($list)->toArray();
            $newlist = [];
            foreach ($list as $k => $v){
                $newlist[$k] = [
                    'id' => $v['id'],
                    'amount' => $v['amount'],
                    'create_time' => date('Y-m-d H:i:s', $v['create_time']),
                    'type' => $v['type'] // 直接获取type字段
                ];
            }
            $this->success('成功', $newlist);
        }else{
            $this->success('成功', []);
        }
    }

    public function setshoukuan(){
        $userinfo = $this->auth->getUserinfo();
        $useraccountModel = new Useraccount();
        $userModel = new \app\admin\model\User();
        Db::startTrans();
        try{
            $wx_account = $this->request->param('wx_account');
            $wx_name = $this->request->param('wx_name');
            $wx_img = $this->request->param('wx_img');
            $zfb_account = $this->request->param('zfb_account');
            $zfb_name = $this->request->param('zfb_name');
            $zfb_img = $this->request->param('zfb_img');
            $sh_account = $this->request->param('sh_account');
            $sh_name = $this->request->param('sh_name');
            $sh_img = $this->request->param('sh_img');
            $bep_wallet_address = $this->request->param('bet_wallet_address');
            $bep_wallet_img = $this->request->param('bep_wallet_img');
            $trc_wallet_address = $this->request->param('trc_wallet_address');
            $trc_wallet_img = $this->request->param('trc_wallet_img');
            if($wx_img =='' && $zfb_img=='' &&$sh_img =='' &&$bep_wallet_img=='' &&$trc_wallet_img==''){
                $this->error('至少要传一种支付方式的二维码');
            }

            $info = $useraccountModel->where(['user_id'=>$userinfo['id']])->find();
            if($info){
                $info = $info->toArray();
                $useraccountModel->where(['id'=>$info['id']])->update([
                    'wx_account'=>$wx_account,
                    'wx_name'=>$wx_name,
                    'wx_img'=>$wx_img,
                    'zfb_account'=>$zfb_account,
                    'zfb_name'=>$zfb_name,
                    'zfb_img'=>$zfb_img,
                    'sh_account'=>$sh_account,
                    'sh_name'=>$sh_name,
                    'sh_img'=>$sh_img,
                    'bep_wallet_address'=>$bep_wallet_address,
                    'bep_wallet_img'=>$bep_wallet_img,
                    'trc_wallet_address'=>$trc_wallet_address,
                    'trc_wallet_img'=>$trc_wallet_img
                ]);
            }else{
                $indata['user_id'] = $userinfo['id'];
                $indata['wx_account'] = $wx_account;
                $indata['wx_name'] = $wx_name;
                $indata['wx_img'] = $wx_img;
                $indata['zfb_account'] = $zfb_account;
                $indata['zfb_name'] = $zfb_name;
                $indata['zfb_img'] = $zfb_img;
                $indata['sh_account'] = $sh_account;
                $indata['sh_name'] = $sh_name;
                $indata['sh_img'] = $sh_img;
                $indata['bep_wallet_address'] = $bep_wallet_address;
                $indata['bep_wallet_img'] = $bep_wallet_img;
                $indata['trc_wallet_address'] = $trc_wallet_address;
                $indata['trc_wallet_img'] = $trc_wallet_img;
                $indata['create_time'] = time();
                $useraccountModel->insert($indata);
            }
            if($wx_img!=''){
                $userModel->where(['id'=>$userinfo['id']])->update(['is_wx'=>1]);
            }
            if($zfb_img!=''){
                $userModel->where(['id'=>$userinfo['id']])->update(['is_zfb'=>1]);
            }
            if($sh_img!=''){
                $userModel->where(['id'=>$userinfo['id']])->update(['is_sh'=>1]);
            }
            if($bep_wallet_img!=''){
                $userModel->where(['id'=>$userinfo['id']])->update(['is_bep'=>1]);
            }
            if($trc_wallet_img!=''){
                $userModel->where(['id'=>$userinfo['id']])->update(['is_trc'=>1]);
            }

            Db::commit();
            $this->success('设置成功');
        }catch(Exception $e){
            Db::rollback();
            $this->error($e->getMessage());
        }
    }

    public function getshoukuan(){
        $userinfo = $this->auth->getUserinfo();
        $useraccountModel = new Useraccount();
        $info = $useraccountModel->where(['user_id'=>$userinfo['id']])->find();
        if($info){
            $info = $info->toArray();
            $this->success('成功',$info);
        }else{
            $this->success('成功',[
                'wx_account'=>'',
                'wx_name'=>'',
                'wx_img'=>'',
                'zfb_account'=>'',
                'zfb_name'=>'',
                'zfb_img'=>'',
                'sh_account'=>'',
                'sh_name'=>'',
                'sh_img'=>'',
                'bep_wallet_address'=>'',
                'bep_wallet_img'=>'',
                'trc_wallet_address'=>'',
                'trc_wallet_img'=>''
            ]);
        }
    }

    public function zhuxiao(){
        $userinfo = $this->auth->getUserinfo();
        $userModel = new \app\admin\model\User();
        $userModel->where(['id'=>$userinfo['id']])->update(['is_zhuxiao'=>1]);
        $this->auth->logout();
        $this->success('注销成功');
    }
}